using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using log4net;


namespace Utility
{

    public class CryptUtility
    {

        private static log4net.ILog logger;

        static CryptUtility()
        {
            CryptUtility.logger = LoggingUtility.GetLogger(typeof(Utility.CryptUtility).FullName);
        }

        private CryptUtility()
        {
        }

        public static string Encrypt(string value)
        {
          return Encryption64.Encrypt(Encryption64.Encrypt(value, "ABCD1234"), "ABCD5678").ToLower();
        }
        /// <summary>
        /// Decode a ecoded string value
        /// </summary>
        /// <param name="encodedvalue"></param>
        /// <returns></returns>
        public static string Decrypt(string encodedvalue)
        {
          return Encryption64.Decrypt(Encryption64.Decrypt(encodedvalue, "ABCD5678"), "ABCD1234");
        }

        private static byte[] getValidIV(string InitVector, int ValidLength)
        {
            byte[] bArr;

            bool flag = InitVector.Length <= ValidLength;
            if (!flag)
                bArr = System.Text.Encoding.ASCII.GetBytes(InitVector.Substring(0, ValidLength));
            else
                bArr = System.Text.Encoding.ASCII.GetBytes(InitVector.PadRight(ValidLength, ' '));
            return bArr;
        }

        private static byte[] getValidKey(System.Security.Cryptography.TripleDESCryptoServiceProvider tdes, string Key)
        {
            string s;

            bool flag = tdes.LegalKeySizes.Length <= 0;
            if (!flag)
            {
                int i = tdes.LegalKeySizes[0].MinSize;
                while (flag)
                {
                    i += tdes.LegalKeySizes[0].SkipSize;
                    flag = ((Key.Length * 8) > i) && (tdes.LegalKeySizes[0].SkipSize > 0) && (i < tdes.LegalKeySizes[0].MaxSize);
                }
                flag = (Key.Length * 8) <= i;
                if (!flag)
                    s = Key.Substring(0, i / 8);
                else
                    s = Key.PadRight(i / 8, ' ');
            }
            else
            {
                s = Key;
            }
            return System.Text.Encoding.ASCII.GetBytes(s);
        }

    } // class CryptUtility

}

